package com.autologistics.socket.service;


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import net.sf.json.JSONObject;

import org.junit.Test;

import com.autologistics.socket.vo.FomatErrorRes;
import com.autologistics.socket.vo.MediaEnableCheckRes;
import com.autologistics.socket.vo.MediaFileConfirmRes;
import com.autologistics.socket.vo.MediaFileDeleteRes;
import com.autologistics.socket.vo.MediaFileUploadRes;
import com.autologistics.socket.vo.MediaTimeRes;
import com.autologistics.socket.vo.StateCheckRes;

public class TestReciveFile {

	private File file = new File("c:/video.mp4");
	@Test
	public void test() {
		file.deleteOnExit();
		try {
			file.createNewFile();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			ServerSocket ss = new ServerSocket(50000);
			while(true){
				Socket socket = ss.accept();
				socket.setSoTimeout(300);
				InputStream is = new BufferedInputStream(socket.getInputStream());

				ByteArrayOutputStream buf = new ByteArrayOutputStream();
				int ch;
				while ((ch = is.read()) >= 0) {
					buf.write(ch);
					if (ch == '}') { // be tolerant (RFC-2616 Section 19.3)
						break;
					}
				}
				if (buf.size() == 0) {
					return;
				}
				String json = buf.toString();
				System.out.println(json);
				try {
					JSONObject jsonObject = JSONObject.fromObject(json);
					String method = jsonObject.optString("requestMethod", "notDefine");
					if(method.equals("statu")){
						StateCheckRes res = new StateCheckRes();
						res.setTotalBlank(70);
						res.setEmpyBlank(20);
						res.setRepairingBlank(20);
						res.setUsedBlank(30);
						
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}else if(method.equals("MediaEnableCheck")){
						MediaEnableCheckRes res = new MediaEnableCheckRes();
						res.setResponseCode(0);
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}else if(method.equals("MediaFileConfirm")){
						MediaFileConfirmRes res = new MediaFileConfirmRes();
						res.setResponseCode(0);
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}else if(method.equals("MediaFileDelete")){
						MediaFileDeleteRes res = new MediaFileDeleteRes();
						res.setResponseCode(0);
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}else if(method.equals("mediaFile")){
						MediaFileUploadRes res = new MediaFileUploadRes();
						res.setResponseCode(0);
						is.skip(4);
						byte data[] = new byte[1024];
						int readlen = 0;
						FileOutputStream fos = new FileOutputStream(file, true);
						try {
							while((readlen = is.read(data))>0){
								fos.write(data, 0, readlen);
							}
						} catch (java.net.SocketTimeoutException e) {
							// TODO Auto-generated catch block
							//e.printStackTrace();
							//do nothing
						}
						fos.close();
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
						
						
					}else if(method.equals("MediaTime")){
						MediaTimeRes res = new MediaTimeRes();
						res.setResponseCode(0);
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}else if(method.equals("notDefine")){
						FomatErrorRes res = new FomatErrorRes();
						res.setResponseDescription("could not find parameter requestMethod");
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}else{
						FomatErrorRes res = new FomatErrorRes();
						res.setResponseDescription("method "+method+" not impement");
						System.out.println(JSONObject.fromObject(res).toString());
						socket.getOutputStream().write(JSONObject.fromObject(res).toString().getBytes());
					}
					socket.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}
